package org.lex.catalog;

import javax.swing.event.EventListenerList;

public abstract class AbstractCatalogModel<T> implements CatalogModel<T> {
	private EventListenerList listenerList = new EventListenerList();

	public void reload() {
		this.setCatalog(this.getCatalog());
	}

	public void addCatalogListener(CatalogListener l) {
		this.listenerList.add(CatalogListener.class, l);
	}

	public void removeCatalogListener(CatalogListener l) {
		this.listenerList.remove(CatalogListener.class, l);
	}

	protected void fireCatalogChanged(T newCatalog) {
		Object[] listeners = listenerList.getListenerList();
		CatalogEvent e = new CatalogEvent(this, newCatalog);
		for (int i = listeners.length - 2; i >= 0; i -= 2) {
			if (listeners[i] == CatalogListener.class) {
				((CatalogListener) listeners[i + 1]).catalogChanged(e);
			}
		}
	}
}
